<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;

class BrandController extends Controller
{
	//  品牌列表
    public function getIndex(Request $request)
    {
    	if($request->input('keywords')){
    		$cate = DB::table('brand')
    		->where('goodsname','like','%'.$request->input('keywords').'%')
    		->select(DB::raw("*,concat(path,'-',id) as paths"))
    		->orderBy('paths')
    		->paginate(5);
    	}else{
        	$cate = DB::table('brand')
        		->select(DB::raw("*,concat(path,'-',id) as paths"))
        		->orderBy('paths')
        		->paginate(5);
    	}
    	$data = $request->all();
    	return view('admin.brand.index',['cate'=>$cate,'data'=>$data]);
    }
    //添加
    public function getAdd()
    {
    	$res = self::getCates();
    	return view('admin.brand.add',['cates'=>$res]);
    }
    //执行添加
    public function postInsert(Request $request)
    {
    	$gid = $request->input('gid');
    	$goodsname = $request->input('name');
    	$data = $request->only(['gid','name']);
    	if($goodsname == ''){
    		return back();
    	}
    	if($gid == 0){
    		$data['path'] = '0';
    	}else{
    		$res = DB::table('brand')->where('id',$gid)->first();
    		$data['path'] = $res->path.'-'.$gid;
    	}
    	$data['date'] = date('Y-m-d H:i:s',time());

    	if(!$request->hasFile('pic')){
	        $data['logo'] = '';
	    }else{
	    	$file = $request->file('pic');
		    if(!$file->isValid()){
		        exit('文件上传出错！');
		    }
		    $suffix = $request->file('pic')->getClientOriginalExtension();
	    	$picname = MD5(time()).'.'.$suffix;
	    	$res = $file->move('./uploads',$picname);
	    	$data['logo'] = $picname;
	    }
    
    	$data['name'] = $request->input('name');
    	
    	$data['date'] = date('Y-m-d H:i:s',time());
    	$rs = DB::table('brand')->insert($data);
		return redirect('/admin/brand/index')->with('success','添加成功');
	}
	//删除
	public function getDelete($id)
	{
		$r = DB::table('brand')->where('id',$id)->delete();
		if($r){
			return back()->with('success','删除成功');
		}else{
			return back()->with('error','删除失败');
		}
	}
	//修改
	public function getEdit($id)
	{
		$catesinfo = DB::table('brand')->where('id',$id)->first();
		return view('admin.brand.edit',['catesinfo'=>$catesinfo]);
	}
	//执行修改
	public function postUpload(Request $request)
	{
    	$id = $request->input('id');
    	$goodsname = $request->input('name');
    	$data = $request->only(['name']);
    	if($goodsname == ''){
    		return back();
    	}
    	$data['date'] = date('Y-m-d H:i:s',time());

    	if(!$request->hasFile('pic')){
	        $data['logo'] = '';
	    }else{
	    	$file = $request->file('pic');
		    if(!$file->isValid()){
		        exit('文件上传出错！');
		    }
		    $suffix = $request->file('pic')->getClientOriginalExtension();
	    	$picname = MD5(time()).'.'.$suffix;
	    	$res = $file->move('./uploads',$picname);
	    	$data['logo'] = $picname;
	    }
    	$data['name'] = $request->input('name');
    	$data['date'] = date('Y-m-d H:i:s',time());
    	$rs = DB::table('brand')->where('id',$id)->update($data);
    	if($rs){
			return redirect('/admin/brand/index')->with('success','修改成功');
		}else{
			return back()->with('error','修改');
		}
	}
    public static function getCates()
	{
		$res = DB::table('brand')
		    ->select(DB::raw("*,concat(path,'-',id) as paths"))
		    ->orderBy('paths')
		    ->get();
		//处理数据
		foreach($res as $k=>$v){
			$arr = explode('-',$v->path);
			$num = count($arr)-1;
			$res[$k]->name = str_repeat('|--',$num).$v->name;
		}
		return $res;
	}
}
